package com.juyu.miliao.api.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.juyu.miliao.common.security.JsonAuthenticationEntryPoint;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

import java.util.Arrays;

import static com.juyu.miliao.api.controller.ExceptionHandlers.NO_LOGIN;

/**
 * @author 老大
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private ObjectMapper objectMapper;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.cors()
                .and()
                .authorizeRequests()
                .antMatchers("/ossKey").permitAll()
                .antMatchers("/rabbitMQ").permitAll()
                .antMatchers("/pay/notify/*").permitAll()
                .antMatchers("/lineTime").permitAll()
                .antMatchers("/userInfo/userList/**").permitAll()
                .antMatchers("/userVideo/list/*/**").permitAll()
//                .antMatchers("/userSort/**").permitAll()
                .antMatchers("/home/video/*").permitAll()
                .antMatchers("/testNetEase/**").permitAll()
                .antMatchers("/version/compare-lowest").permitAll()
                .antMatchers("/version/ios").permitAll()
                .antMatchers("/type/ios").permitAll()
                .antMatchers("/h5/**").permitAll()
                .antMatchers("/newYearDayActivity/**").permitAll()
                .antMatchers("/dynamic/share/textContent/**").permitAll()
                .antMatchers("/appkey/update/*").permitAll()
                .antMatchers("/makeMoney/**").permitAll()
                .antMatchers("/userInfo/qualityHostList/*/**","/other/pay","/other/openId",
                        "/other/wxPay_notify/*","/wxBind/code/phone","/wxBind/index",
                        "/wx/wxLogin/**","/wx/openId/**","/wxBind/gold","/wx/share/openId",
                        "/advertisement/switch","/appkey/key/{key}","/appkey/list/{key}","/find/dynamic/{page}-{pageSize}",
                        "/find/dynamic/detail/{findId}","/find/dynamic/comment/{findId}/{page}-{pageSize}",
                        "/find/dyanmic/commentreply/{findId}/{page}-{pageSize}","/testUserLevel/init",
                        "/testFans/init","/testComment/init","/userVip/compensate","/userInfo/check/phone/{phoneNum}",
                        "/initHostRecentDevelopments","/deleteHostRecentDevelopments").permitAll()
                //swagger ui
                .antMatchers("/v2/api-docs", "/configuration/ui", "/swagger-resources", "/configuration/security",
                        "/swagger-ui.html", "/webjars/**", "/swagger-resources/configuration/ui", "/swagger-ui.html",
                        "/doc.html", "/docs.html"
                ).permitAll()
                .anyRequest().authenticated()
                .and()
                .csrf().disable()
                .exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint())
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.NEVER)
                .and()
                .cors().configurationSource(corsConfigurationSource());
    }

    @Bean
    public AuthenticationEntryPoint authenticationEntryPoint() {
        JsonAuthenticationEntryPoint authenticationEntryPoint = new JsonAuthenticationEntryPoint(objectMapper,
                NO_LOGIN);
        return authenticationEntryPoint;
    }

    @Bean
    public CorsConfigurationSource corsConfigurationSource(){
        CorsConfiguration configuration = new CorsConfiguration();
        configuration.setAllowedOrigins(Arrays.asList("*"));
        configuration.setAllowedMethods(Arrays.asList("*"));
//        configuration.setAllowedHeaders(Arrays.asList("X-Auth-Token", "Origin", "X-Requested-With", "Content-Type", "Accept", "User-Token", "Channel-Number"));
        configuration.setAllowedHeaders(Arrays.asList("*"));
        configuration.setAllowCredentials(false);
        configuration.addExposedHeader("X-Auth-Token, Location");
        UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**", configuration);
        return corsConfigurationSource;
    }

}
